﻿using CampDay20.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CampDay20.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            List<Student> students;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                students = db.Students.Where(x => x.DateDeleted == null).ToList();
            }
            return View(students);
        }

        public ActionResult Student(int id)
        {
            Student model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                //essays refers to the 'essays' property on the Student Class
                model = db.Students.Include("Essays").FirstOrDefault(x => x.Id == id);
            }
            return View(model);
        }

        public ActionResult CreateStudent()
        {
            return View(new Student());
        }

        [ValidateAntiForgeryToken]
        [HttpPost]
        public ActionResult CreateStudent(Student model)
        {
            if (ModelState.IsValid)
            {
                using (ApplicationDbContext db = new ApplicationDbContext())
                {
                    model.DateCreated = DateTime.Now;
                    model.DateDeleted = null;
                    db.Students.Add(model);
                    db.SaveChanges();
                }
                return RedirectToAction("Index");
            }
            return View(model);
        }

        public ActionResult DeleteStudent(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Student stu = db.Students.FirstOrDefault(x => x.Id == id);
                stu.DateDeleted = DateTime.Now;
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        public ActionResult EditStudent(int id)
        {
            Student model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Students.FirstOrDefault(x => x.Id == id);
            }
            return View("CreateStudent", model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult EditStudent(Student model)
        {
            if (ModelState.IsValid)
            {
                using (ApplicationDbContext db = new ApplicationDbContext())
                {
                    Student student = db.Students.FirstOrDefault(x => x.Id == model.Id);
                    student.FirstName = model.FirstName;
                    student.LastName = model.LastName;
                    student.Grade = model.Grade;
                    db.SaveChanges();
                }
                return RedirectToAction("Index");
            }
            return View("CreateStudent", model);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}